<?php

namespace App\Http\Controllers;


use App\Models\AdminUser;
use App\Models\ProblemFeedback;
use App\Models\User;
use App\Models\BrokerCompany;
use App\Models\HouseBroker;
use App\Models\LiveStreaming;
use App\Models\WxUser;
use App\Services\BaiduAudit;
use App\Services\LoginServices;
use App\Tools\JsonTools;
use Illuminate\Hashing\BcryptHasher;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    /**
     * 静默登录
     */

    public function userLogin(Request $request)
    {

        $code = $request->input('code');

        $token = LoginServices::getToken($code);

        return JsonTools::JsonSuccess($token, 200, '登录成功');
    }

    /**
     * 获取验证码
     * @param Request $request
     * @return array
     */

    public function getCaptcha(Request $request)
    {

        $phone = $request->input('phone');

        $rand = LoginServices::sendNote($phone);

        return JsonTools::JsonError($rand, 200, '获取验证码');
    }

    /**
     * 手机验证码登录
     * @param Request $request
     * @return array
     */

    public function phoneLogin(Request $request)
    {

        $phone = $request->input('phone');

        $verification = $request->input('verification');

        $code = $request->input('code');

        $res = LoginServices::loginPhone($phone, $verification);

        return $res;
    }

    /**
     * 微信授权登录
     * @param Request $request
     * @return array
     */

    public function authorizationLogin(Request $request)
    {

        $code = $request->input('code');

        $nickName = $request->input('nickname');

        $headPortrait = $request->input('headportrait');
//        微信授权
        $res = LoginServices::wxLogin($code, $nickName, $headPortrait);

        return JsonTools::JsonSuccess($res, 200, '微信授权登录');
    }

    /**
     * Notes:qq 授权登录回调
     * User: 齐硕
     * Date: 2022/11/14
     * Time:14:12
     */
    public function callback(Request $request)
    {
        require_once("./plugins/qq/API/qqConnectAPI.php");
        $qc = new \QC();
//        获取access_token和openid
        $access_token = $qc->qq_callback();
        $openid = $qc->get_openid();
//        获取用户信息
        $qc = new \QC($access_token, $openid);
        $info = $qc->get_user_info();
//        dump($info);
//        die();
        $user = User::where('open_id', $openid)->first();
        if ($user) {
            $user->name = $info['nickname'];
            $user->username = $info['nickname'];
            $user->avatar = $info['figureurl_2'];
            $user->save();

        } else {
            User::create(['username' => $info['nickname'], 'password' => bcrypt('admin'), 'name' => $info['nickname'], 'open_id' => $openid, 'avatar' => $info['figureurl_2']]);

        }
//        return admin_redirect('/');
//        return redirect("http://www.qishuo.xyz/auth/login"."?username={$user['name']}&"."password={$user['password']}"au
        Auth::login($user, true);
//        $actives=$user->actives;
//        dump($actives);
//        return redirect('/admin');
        header("Location: http://www.qishuo.xyz/admin");
    }

//根据地区展示不同的轮播图
    public function show(Request $request)
    {
        $address = $request->get('address');
        $show = new \App\Services\tactics\Context($address);
        $data = $show->dodo();
        return JsonTools::JsonSuccess($data);
    }

    /**
     * 获取用户信息
     * @param Request $request
     * @return array
     */


    public function userMessage(Request $request)
    {

        $id = $request->id;

        $user = WxUser::where('id', $id)->first();

        $data = [
            'user_name' => $user['user_name'],
            'user_image' => $user['user_image'],
            'im_id' => $user['im_id']
        ];

        return JsonTools::JsonSuccess($data, 200, '用户信息');

    }

    /**
     * 经纪人展示
     * @param Request $request
     * @return array
     */

    public function brokerMessage(Request $request)
    {

        $id = $request->id;
        $broker = HouseBroker::where('id', $id)->first();

        $live = LiveStreaming::where('uid', $broker['id'])->first();
        if ($live) {
            $liveId = $live['id'];
        } else {
            $liveId = 0;
        }
        $user = [
            "broker_name"=>$broker['broker_name'],
            "broker_image"=>$broker['broker_image'],
            'id'=>$liveId,
            'new_house'=>$broker['new_house'],
            'two_house'=>$broker['two_house'],
            'rental_housing'=>$broker['rental_housing']
        ];
        return JsonTools::JsonSuccess($user, 200, '经纪人信息');

    }

    /**
     * Notes: 小程序问题反馈
     * User: 齐硕
     * Date: 2022/11/21
     * Time:20:39
     * @param Request $request
     * @return array|void
     */
    public function problemFeedback(Request $request)
    {
        $content = $request->post('content');
        $text = BaiduAudit::textAudit($content);
        if ($text) {
            return ['code' => 2001, 'msg' => $text, 'data' => []];
        } else {
            $data = [
                'uid' => $request->id,
                'phone' => $request->post('phone'),
                'content' => $content,
                'model' => $request->post('model'),
                'system' => $request->post('system'),
                'platform' => $request->post('platform'),
                'status' => 0
            ];
            $create = ProblemFeedback::create($data);
            if ($create) {
                return JsonTools::JsonSuccess($create);
            }
        }
    }


}
